package cn.ylz.ylmall.shopping.dao.persist.repository.impl;

import cn.ylz.ylmall.shopping.dao.persist.mapper.ShoppingMapper;
import cn.ylz.ylmall.shopping.dao.persist.repository.IShoppingRepository;
import cn.ylz.ylmall.shopping.pojo.entity.Shopping;
import cn.ylz.ylmall.shopping.pojo.param.ShoppingQuantityParam;
import cn.ylz.ylmall.shopping.pojo.vo.ShoppingVO;
import cn.ylz.ylmall.shopping.pojo.vo.ShoppingVO1;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.lang.reflect.Type;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
@Slf4j
@Repository
public class ShoppingRepositoryImpl implements IShoppingRepository {
    @Autowired
    private ShoppingMapper shoppingMapper;

    @Override
    public int insert(Shopping shopping) {
        log.debug("开始插入数据");
        List<ShoppingVO> list = shoppingMapper.showAll(shopping.getUserId());
        if(!list.isEmpty()){
            for (int i = 0; i < list.size(); i++) {
                if(shopping.getProductId().equals(list.get(i).getProductId())){
                    int quantity = shopping.getQuantity()+list.get(i).getQuantity();
                    BigDecimal totalPrice = shopping.getTotalPrice().add(list.get(i).getTotalPrice());
                    shoppingMapper.updateAddQuantity(list.get(i).getCartId(),quantity,totalPrice);
                    return 0;
                }
            }
        }
            return shoppingMapper.insert(shopping);

    }
    public List<ShoppingVO> showAll(Long userId){

        return shoppingMapper.showAll(userId);
    }

    public void updateQuantity(List<ShoppingQuantityParam> lis){

        for (int i = 0; i < lis.size(); i++) {
            shoppingMapper.updateQuantityByCartId(lis.get(i).getCartId(),lis.get(i).getQuantity());
            shoppingMapper.updateSelected(lis.get(i).getCartId(),lis.get(i).getSelected());
            Shopping shopping = shoppingMapper.selectByCartId(lis.get(i).getCartId());
            BigDecimal totalPrice = shopping.getPrice().multiply(new BigDecimal(lis.get(i).getQuantity()));
            shoppingMapper.updateTotalPrice(lis.get(i).getCartId(),totalPrice);
        }

    }
    public void delete(List<Long> list){

        for (int i = 0; i < list.size(); i++) {
            shoppingMapper.delete(list.get(i));
        }

    }

    @Override
    public List<ShoppingVO> showDeclineAll(Long userId) {
        return shoppingMapper.showDeclineAll(userId);
    }

    @Override
    public List<ShoppingVO> showHavingAll(Long userId) {
        return shoppingMapper.showHavingAll(userId);
    }

    @Override
    public List<ShoppingVO> selectCartId(List<Long> list) {
        List<ShoppingVO> arr = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            arr.add(shoppingMapper.selectCartId(list.get(i)));

        }
        return arr;
    }

    @Override
    public void updateNumber(Long cartId) {

        Shopping shopping = shoppingMapper.selectByCartId(cartId);

        int i = shopping.getNumber()+1;
        shoppingMapper.updateNumber(cartId,i);


    }


}
